package com.gulimall.product.web;

import com.gulimall.common.vo.product.Catalogs2Vo;
import com.gulimall.product.domain.entity.Category;
import com.gulimall.product.service.ICategoryService;
import org.redisson.api.RCountDownLatch;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Resource
    private ICategoryService categoryService;
    @Resource
    private RedissonClient redissonClient;


    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){
        //查询商品一级分类
        List<Category> categoryList = categoryService.getLevel1Categorys();
        model.addAttribute("categorys",categoryList);
        return "index";
    }

    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catalogs2Vo>> getCatalogJson() throws InterruptedException {
        Map<String, List<Catalogs2Vo>> map = categoryService.getCatalogJson();
        return map;
    }

    /**
     * 测试redisson的闭锁
     */
    @GetMapping("/rest")
    @ResponseBody
    public String rest() throws InterruptedException {
        RCountDownLatch lock = redissonClient.getCountDownLatch("restLock");
        lock.trySetCount(5);
        lock.await();
        return "放假了--";
    }

    @GetMapping("/gogogo/{classId}")
    @ResponseBody
    public String gogogo(@PathVariable("classId") Long classId) {
        RCountDownLatch lock = redissonClient.getCountDownLatch("restLock");
        lock.countDown();
        return classId + "班人都走了";
    }
}
